package com.components.project

import android.text.TextUtils
import android.view.View
import android.widget.FrameLayout
import com.blankj.utilcode.util.ToastUtils
import com.components.base.titlebar.OnTitleBarListener
import com.components.base.titlebar.TitleBar
import com.components.project.databean.NewsDataBean
import com.components.project.ui.base.ProjectBaseActivity
import com.components.project.ui.base.ProjectBaseFragment
import com.components.project.web.WebViewIntentParam

/**
 * @Author LuoKun
 * @Des
 */
class X5Fragment : ProjectBaseFragment<ProjectBaseActivity>(),OnTitleBarListener{

    private val flContainer: FrameLayout by lazy { findViewById(R.id.flContainer) }
    private val titleBar: TitleBar by lazy {
        findViewById(R.id.titleBar)
    }
    var x5WebView: X5WebView? = null

    override fun getLayoutId(): Int {
        return R.layout.fragment_x5;
    }

    override fun initView() {
        getIntentParam()
    }

    override fun initData() {

    }

    private fun getIntentParam() {
        /**获取配置信息和数据源信息*/
        val bundle = arguments
        var webIntentParam: WebViewIntentParam? = null
        bundle?.getSerializable("webIntentParam")?.let {
            webIntentParam = it as WebViewIntentParam
        }
        val newsData = bundle?.getSerializable("newsData") as NewsDataBean
        /**配置web加载*/
        x5WebView = activity?.let { X5WebView(it, null, newsData.url) }
        flContainer.addView(
            x5WebView, FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT
            )
        )

        webIntentParam?.let {
            if (!TextUtils.isEmpty(it.titleText)) {
                titleBar.setTitle(it.titleText)
            }
            if (it.isHideTop) {
                titleBar.visibility = View.GONE
            } else {
                titleBar.visibility = View.VISIBLE
            }
        } ?: let {
            titleBar.visibility = View.VISIBLE
            titleBar.setTitle(newsData.title)
        }

    }

    override fun onLeftClick(v: View) {
        finish()
    }

    override fun onTitleClick(v: View) {
        ToastUtils.showShort("${(v.findViewById<TitleBar>(R.id.titleBar)).getTitle()}")
    }

    override fun onRightClick(v: View) {
        ToastUtils.showShort("我是右侧点击事件")
    }

}